<!--
* description: 门店资金类型维护弹窗
* author: liyanm
* createdDate: 2019-08-06
*  2019-08-10 添加保存校验 liyanm
-->
<template>
    <one-table-template
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsInitTable="true"
      :key="popupsTableKey"
      :callbackObj="callbackObj"
      :dynamicPageSize="dynamicPageSize"
    />
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { orgApis } from "@/api/apiList/org";
import OneTableTemplate from "@/components/templates/popupsOneTable";
export default {
  name: "orgmaintain",
  // 组件混入对象
  mixins: [ oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate
  },
  props: {
    showdlrId1: {
      type: String,
      default: function() {
        return "";
      }
    }
  },
  data() {
    var popupsPageCode = "orgDlrSalesEdit-Maintain"
    return {
      // 查询API配置对象
      apiConfig: orgApis.mdmOrgDlrLinkInfoQueryList,
      popupsPageCode:"orgDlrSalesEdit-Maintain",
      rowData: {},
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey5",
          type: "primary",
          size: "small",
          name: "search",
          clickEvent: () => this.queryTable1(1),
          text: "查询全部"
        },
        {
          compKey: "btnKey1",
          type: "",
          size: "small",
          name: "search",
          clickEvent: () => this.querys(1),
          text: "查询"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          name: "preservation",
          clickEvent: () => this.save(),
          text: "保存"
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          name: "cancel",
          clickEvent: () => this.delete(),
          text: "删除"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          clickEvent: () => this.reset(),
          text: "重置"
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[popupsPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[popupsPageCode].formComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[popupsPageCode].formComponents
          : [
        // {
        //   compKey: "compKey0",
        //   labelName: "经销商Id",
        //   codeField: "dlrId",
        //   // isRequire: true,
        //   component: () => import("@/components/org/commonInput"),
        //   type: "inputText",
        //   isMust: true,
        //   isShow: "false",
        //   hidden: true
        //   // disabled: this.curPopupsState === "true"
        //   // disabled: this.rowData == null ? false : true,
        // },
        {
          compKey: "compKey1",
          labelName: "类型：",
          codeField: "linkManType",
          isRequire: false,
          lookuptype: "SE0102",
          component: () => import("@/components/org/LookupValue"),
          type: "	dropdownList",
          isMust: true
        },
        {
          compKey: "compKey2",
          labelName: "姓名：",
          codeField: "linkManName",
          isRequire: false,
          component: () => import("@/components/org/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey3",
          labelName: "手机：",
          codeField: "linkManMobil",
          isRequire: true,
          validrule: { isformat: /^1[3456789]\d{9}$/, noRequire: false },
          component: () => import("@/components/org/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey4",
          labelName: "电话：",
          codeField: "linkManPhone",
          // isRequire: true,
          component: () => import("@/components/org/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey5",
          labelName: "EMAIL：",
          codeField: "email",
          // isRequire: true,
          component: () => import("@/components/org/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey6",
          labelName: "邮编：",
          codeField: "zip",
          // isRequire: true,
          component: () => import("@/components/org/commonInput"),
          type: "inputText",
          isMust: true
        }
      ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[popupsPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[popupsPageCode].tableCols.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[popupsPageCode].tableCols
          : [ 
        {
          prop: "linkManType",
          label: "类型编码",
          width: null,
          align: "center"
        },
        {
          prop: "linkManTypeName",
          label: "类型名称",
          width: null,
          align: "center"
        },
        {
          prop: "linkManName",
          label: "姓名",
          width: null,
          align: "center"
        },
        {
          prop: "linkManMobil",
          label: "手机",
          width: null,
          align: "center"
        },
        {
          prop: "linkManPhone",
          label: "电话",
          width: null,
          align: "center"
        },
        {
          prop: "email",
          label: "EMAIL",
          width: null,
          align: "center"
        },
        {
          prop: "zip",
          label: "邮编",
          width: 80,
          align: "center"
        },
        {
          prop: "dlrLinkInfoId",
          label: "专营店联系人ID",
          width: null,
          align: "center",
          hidden: true
        },
        {
          prop: "dlrId",
          label: "经销商Id",
          width: null,
          align: "center",
          hidden: true
        },
        {
          prop: "updateControlId",
          label: "并发控制Id",
          hidden: true,
          align: "center"
        }
      ],
      queryFormField:{
        dlrId: this.showdlrId1,
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        dlrId: this.showdlrId1,
        linkManType: "",
        linkManName: "",
        linkManMobil: "",
        linkManPhone: "",
        email: "",
        zip: ""
        // dlrLinkInfoId: ""
      },
      // 去掉不需要传的查询字段
      // unFormField: {
      //   linkManMobil: "",
      //   linkManPhone: "",
      //   email: "",
      //   zip: ""
      // },
      updateControlId: "",
      dlrLinkInfoId: "",
      callbackObj:{
        onRowClick:this.rowClickModel
      },
      popupsTableKey:"popupsTableKey"
    };
  },
  methods: {
    querys(){
      this.queryTable(1)
    },
    queryTable1(page) {
      const that = this.$refs.multipleTable;
      that.currentRow = null;
      that.listLoading = true;
      const queryObj = {
        // api配置
        apiConfig: orgApis.mdmOrgDlrLinkInfoQueryList, 
        // apiConfig: veApis.veBusinessConfirmation,
        // 需要调用的API服务配置
        apiServices: [
          {
            //表格中台返回网格的字段
            apiQueryRow: ["linkManType", "linkManTypeName", "linkManName"
            , "linkManMobil"
            , "email"
            , "linkManPhone"
            , "zip"
            , "dlrLinkInfoId"
            , "dlrId"
            , "updateControlId"
            ]
          }
        ],
        //条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          //当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo:  this.queryFormField
        }
      };
      //转换了中台请求格式数据
      var paramD = that.$getParams(queryObj);
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (
          response.result === "1" &&
          response.rows
        ) {
          if (page) {
            //查询完返回指定的page页数
            // that.listQuery.pageIndex = page;
          }
          that.pageTotal =
            response.records;
          that.list = response.rows;
          that.listLoading = false;
        }
      });
    },
    queryTables(){
      this.tableComponents[2].isRequire = false
      this.queryTable(1)
    },
    reset() {
      this.formField.linkManType = ""
      this.formField.linkManName = ""
      this.formField.linkManMobil = ""
      this.formField.linkManPhone = ""
      this.formField.email = ""
      this.formField.zip = ""
      this.formField.dlrId = this.showdlrId1
      this.updateControlId = "";
      this.dlrLinkInfoId = "";
    },
    // sendCode(type) {
    //   this.rowData = {};
    //   this.$emit("close", type);
    // },
    // 赋值给下方表单
    rowClickModel(row) {
      this.rowData = row;
      this.formField.linkManType = row.linkManType
      this.formField.linkManName = row.linkManName
      this.formField.linkManMobil = row.linkManMobil
      this.formField.linkManPhone = row.linkManPhone
      this.formField.email = row.email
      this.formField.zip = row.zip
      this.formField.dlrId = row.dlrId
      this.updateControlId = row.updateControlId;
      this.dlrLinkInfoId = row.dlrLinkInfoId;
    },
    //删除
    delete() {
      // debugger;
      const that = this;

      if (that.selecRowtData == "") {
        this.$message({
          message: this.$t("org.message.moreOneData") /*请至少选择一条数据*/,
          type: "warning"
        });
        return false;
      }
      let queryObj = {
        //保存需要传 type="mutation"
        type: "mutation",
        // api配置
        apiConfig: orgApis.mdmOrgDlrLinkInfoMutationDelete,
        // 需要调用的API服务配置
        apiServices: [
          {
            //表格中台返回网格的字段
            apiQueryRow: []
          }
        ],
        //条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          pageSize: 10,
          pageIndex: 1,
          //当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: {
            dlrLinkInfoId: that.formField.dlrLinkInfoId,
            updateControlId: that.updateControlId,
            dlrLinkInfoId: that.dlrLinkInfoId
          }
        }
      };
      //转换了中台请求格式数据
      var paramD = that.$getParams(queryObj);
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then(response => {
        // console.log(response);
        if (response.result === "1") {
          // dlrId: that.formField.dlrId = ""; //从主页表格行数据获取带回编辑页面用作此保存，目前中台还没定义该字段。
          that.formField.linkManType = "";
          that.formField.linkManName = "";
          that.formField.linkManMobil = "";
          that.formField.linkManPhone = "";
          that.formField.email = "";
          that.formField.zip = "";
          that.updateControlId = "";
          that.dlrLinkInfoId = "";
          this.updateControlId = "";
          this.dlrLinkInfoId = "";
          that.$message({
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.sendCode("1");
        } else {
          that.$message({
            message: response.data.mdmOrgDlrLinkInfoMutationDelete.msg,
            type: "warning",
            duration: 2000
          });
        }
        this.queryTable1();
      });
    },
    // 下方联系人保存
    save() {
      this.$utils.validataMoth(this, "validpopup");
      if (this.valid) {
        let that = this;
        // if (JSON.stringify(this.rowData) == "{}") {
        //   that.$message({
        //     message: this.$t("org.message.selOneData") /*请选择一条数据*/,
        //     type: "warning",
        //     duration: 2000
        //   });
        //   return false;
        // }
        if(this.formField.linkManType == "") {
          that.$message({
            message: "类型不能为空" /*请选择一条数据*/,
            type: "warning",
            duration: 2000
          });
          return false;
        }
        if(this.formField.linkManName == "") {
          that.$message({
            message: "姓名不能为空" /*请选择一条数据*/,
            type: "warning",
            duration: 2000
          });
          return false;
        }
        if(this.formField.linkManMobil == "") {
          that.$message({
            message: "手机不能为空" /*请选择一条数据*/,
            type: "warning",
            duration: 2000
          });
          return false;
        }
        let queryObj = {
          // 保存mutation
          type: "mutation",
          typeParam: "($dataInfo:[InputMdmOrgDlrLinkInfo])",
          // api配置
          apiConfig: orgApis.mdmOrgDlrLinkInfoMutationSaveById,
          apiServices: [
            {
              apiServiceParam: "(dataInfo:$dataInfo)"
            }
          ],
          variables: {
            //当前中台使用的名称有dataInfo、info，具体查看API文档
            dataInfo: {
              dlrId: that.formField.dlrId, //从主页表格行数据获取带回编辑页面用作此保存，目前中台还没定义该字段。
              linkManType: that.formField.linkManType,
              linkManName: that.formField.linkManName,
              linkManMobil: that.formField.linkManMobil,
              linkManPhone: that.formField.linkManPhone,
              email: that.formField.email,
              zip: that.formField.zip,
              updateControlId: that.updateControlId,
              dlrLinkInfoId: that.dlrLinkInfoId
            }
          }
        };
        //转换了中台请求格式数据
        var paramD = that.$getParams(queryObj);
        // 调用中台API方法（可复用）
        that.$requestAPI(paramD).then(response => {
          if (response.result === "1") {
            that.$message({
              message: "保存成功",
              type: "success",
              duration: 2000
            });
            this.sendCode("1");
          } else {
            that.$message({
              message: response.msg,
              type: "warning",
              duration: 2000
            });
          }
          that.reset()
          that.queryTable()
        });
      }
    }
  }
};
</script>
